package com.eschool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eschool.mapper.StudentMapper;
import com.eschool.pojo.StudentChangeDTO;
import com.eschool.pojo.StudentDAO;
import com.eschool.pojo.UserDAO;
import com.eschool.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;


    @Override
    public StudentDAO Login(String studentId, String password) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("password",password);
        queryWrapper.eq("studentId",studentId);

        StudentDAO studentDAO = studentMapper.selectOne(queryWrapper);
        System.out.println(studentDAO);
        return studentDAO;
    }

    @Override
    public StudentDAO findById(String studentId) {
        QueryWrapper  queryWrapper = new QueryWrapper();
        queryWrapper.eq("studentId",studentId);
        StudentDAO studentDAO = studentMapper.selectOne(queryWrapper);
        return studentDAO;
    }

    @Override
    public int StudentChangeInfo(String studentId, StudentChangeDTO studentChangeDTO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("studentId",studentId);
        StudentDAO studentDAO = studentMapper.selectOne(queryWrapper); //先查出来要修改的用户

        BeanUtils.copyProperties(studentChangeDTO,studentDAO);
        int updaterow = studentMapper.update(studentDAO,queryWrapper);
        return updaterow;
    }

    @Override
    public int StudentChangePassword(String studentId, String oldPassword, String newPassword) {
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("studentId",studentId);
        StudentDAO studentDAO = studentMapper.selectOne(queryWrapper1);

        if(studentDAO.getPassword().equals(oldPassword)){
            studentDAO.setPassword(newPassword);
            int updateRow = studentMapper.update(studentDAO,queryWrapper1);
            return updateRow;
        }
        else{
            return -1;
        }
    }
}
